const values_default = {}

const domain_default = {}

const fields_default = {
  state: null,
  name: null,
  origin: null,
  date_order: null,
  date_approve: null,
  partner_id: null,
  partner_ref: null,
  user_id: null,
  company_id: null,
  date_planned: null,
  amount_total: null,
  invoice_status: null,
  invoice_count: null,
  invoice_ids: null,
  product_id: null,

  order_line: {
    product_id: null,
    name: null,
    price_unit: null,
    product_qty: null,
    qty_received: null,
    qty_invoiced: null,
    price_subtotal: null
  }
}

const filterItems = [
  { type: 'input', name: 'name___like', placeholder: '单号' },
  { type: 'date', name: 'date_order', placeholder: '日期' },
  { type: 'select', name: 'partner_id', placeholder: '供应商' }
]

const columns = [
  { name: 'company_id__name', type: 'char', label: '公司' },
  { name: 'state__name', type: 'char', label: '状态' },
  { name: 'invoice_status__name', type: 'char', label: '开票状态' },
  { name: 'name', type: 'char', label: '订单号' },
  { name: 'date_order', type: 'char', label: '日期' },
  { name: 'date_approve', type: 'char', label: '确认日期' },
  { name: 'date_planned', type: 'char', label: '计划收货日期' },
  { name: 'partner_id__name', type: 'char', label: '供应商' },
  { name: 'user_id__name', type: 'char', label: '采购员' },
  { name: 'amount_total', type: 'char', label: '金额' }
]

const formItems = [
  { type: 'date', name: 'date_order', label: '日期' },
  {
    type: 'select',
    name: 'partner_id',
    label: '供应商',
    placeholder: '供应商'
  },
  { type: 'select', name: 'user_id', label: '采购员', placeholder: '采购员' }
]

const child_configs = {
  order_line: {
    columns: [
      { name: 'product_id__name', type: 'char', label: '服务' },
      { name: 'name', type: 'char', label: '说明' },
      { name: 'product_qty', type: 'char', label: '数量' },
      { name: 'qty_received', type: 'char', label: '已接收数量' },
      { name: 'qty_invoiced', type: 'char', label: '已开票数量' },
      { name: 'price_unit', type: 'char', label: '单价' },
      { name: 'price_subtotal', type: 'char', label: '小记' }
    ],

    formItems: [
      { type: 'input', name: 'name', label: '说明' },
      {
        type: 'select',
        name: 'product_id',
        label: '产品',
        placeholder: '产品'
      },
      {
        type: 'number',
        name: 'price_unit',
        label: '单价',
        precision: 2,
        step: 1,
        default: 0,
        placeholder: '单价'
      },
      {
        type: 'number',
        name: 'product_qty',
        label: '数量',
        precision: 2,
        step: 1,
        default: 0,
        placeholder: '数量'
      }
    ]
  }
}

const workflow = {
  states: {
    draft: '询价单',
    sent: '已发送',
    to_approve: '已批准',
    purchase: '采购订单',
    done: '已完成',
    cancel: '已取消'
  },

  actions: {
    update: { value: '', name: '修改', type: 'primary' },
    to_invoice: {
      name: '财务开票',
      method: 'to_invoice',
      last_in: ['purchase'],
      // hidden: params => {
      //   // eslint-disable-next-line object-curly-spacing
      //   const { record = {} } = params
      //   return record.invoice_status !== 'to invoice'
      // },
      odoo_type: 'new'
    },
    draft: {
      name: '设为询价单',
      method: 'button_draft',
      last_in: ['cancel', 'purchase'],
      odoo_type: 'object',
      type: 'primary'
    },
    sent: {
      name: '发送给客户',
      method: 'action_sent',
      last_in: ['draft'],
      odoo_type: 'object',
      type: 'primary'
    },
    purchase: {
      name: '确认',
      method: 'button_confirm',
      last_in: ['draft', 'sent'],
      odoo_type: 'object',
      type: 'primary'
    },
    cancel: {
      // TBD 已经开票的不能取消
      name: '取消',
      last_in: ['draft', 'sent', 'purchase'],
      method: 'button_cancel',
      odoo_type: 'object'
    }
  },

  transfers: {
    draft: {
      actions: ['update', 'sent', 'purchase'],
      states: ['draft', 'sent', 'purchase']
    },
    sent: {
      actions: ['draft', 'purchase', 'cancel'],
      states: ['draft', 'sent', 'purchase']
    },
    purchase: {
      actions: ['cancel', 'to_invoice'],
      states: ['draft', 'sent', 'purchase']
    },
    cancel: {
      actions: ['draft'],
      states: ['draft', 'cancel']
    }
  }
}

const Model = {
  configs: {
    workflow,
    values_default,
    domain_default,
    fields_default,
    filterItems,
    columns,
    formItems,
    child_configs
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain_src = {}) {
        // console.log('this.global_domain in odoo.res.partner')
        const domain = { ...domain_src }
        // const userinfo = self.get_userinfo()
        // const { uid } = userinfo
        // domain.user_id = uid
        return super.global_domain(domain)
      }

      async action_sent(rid) {
        await this.write({ id: rid, state: 'sent' })
        return true
      }

      async to_invoice(rid) {
        const context = { create_bill: true }
        const action = await this.call('action_view_invoice', [rid], {
          context
        })

        const fields = {
          name: null,
          partner_id: null,
          invoice_line_ids: {
            product_id: null,
            name: null,
            account_id: null,
            price_unit: null,
            quantity: null
          }
        }

        const move_obj = this.env('account.move')
        const res = await move_obj.call('new2', [{}], {
          context: action.context,
          onchange: ['_onchange_purchase_auto_complete'],
          fields: {}
        })
        return res
      }
    }
    return ModelClass
  }
}

export default Model
